/**
 * 修改手机号
 */
var getyzm_able = true;
var localStorageManagers = require("script/localStorageManagers.js");
var downloadUtils = require('scripts/Core/DownLoadUtil.js');
var mobileFrame = require('scripts/Core/MobileFrame.js');
//mui初始化
mui.init({
	swipeBack: true
});
//plus初始化
mui.plusReady(plusReady);
//plusReady
function plusReady() {
	//获取验证码
	mui('body').on('tap', '#codebtn', function() {
		if (getyzm_able == true) {
			var mobile = document.getElementById('mobile').value;
			//手机格式
			if (!isFormat(mobile))
				return;
			//该手机号是否已绑定
			var url = serverAjax + 'mobile/user/isBindMobile.do';
			mui.ajax(url, {
				data: {
					mobile: mobile
				},
				type: 'post',
				dataType: 'json',
				timeout: 10000,
				success: function(data) {
					if (data != null && data.code == 1) {
						this.className = 'yb';
						timedCount();
						sendCheckcode(mobile);
					} else {
						mui.toast('当前手机号尚未绑定帐号');
					}

				},
				error: function(e1, e2, e3) {
					ajaxErrorTip();
					console.log(e1);
				}
			});

		}
	});
	//下一步
	mui('body').on('tap', '.nextsubmit', nextStep);
	//确定
	mui('body').on('tap', '.submit', savePassword);
};
//发送验证码
var issend=false;
function sendCheckcode(mobile) {
	//var tips=isFormat(mobile);
	//if(tips!=''&&tips.length>0){
	//	mui.toast(tips);return;
	//}
	var userid = localStorageManagers.getUseridSession();
	var url = serverAjax + 'mobile/user/getMobileTelCheckCode2.do';
	mui.ajax(url, {
		data: {
			userid: userid,
			mobile: mobile
		},
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
			 if(data.responseCode==0){
			 	issend=true;
			 document.getElementById('fMobile').value = mobile;
		     mui.toast('发送成功');
		  }else{
		  	 mui.toast(data.responseInfo);
		  }
			
			//if(data==null||data.length==0)return;
		},
		error: function(e1, e2, e3) {
			ajaxErrorTip();
			console.log(e1);
		}
	});
};

function isFormat(mobile) {
	var isCheck = true;
	if (mobile.length > 0) {
		var re = /(^1[3|5|8][0-9]{9}$)/;
		var tips = '';
		if (trim(mobile) != '') {
			if (!re.test(mobile)) {
				mui.toast('请输入正确的手机号码');
				isCheck = false;
			}
		}
	} else {
		mui.toast("请输入手机号");
		isCheck = false;
	}
	return isCheck;
}
//计时
function timedCount() {
	var interval = 1000;
	var len = 60000;
	if (codebtn.value == null || codebtn.value < 10 || codebtn.value == len) {
		codebtn.value = len;
		getyzm_able = false;
		codebtn.innerHTML = (len / 1000) + "秒后重发";
	}
	var i = codebtn.value - interval;
	if (i <= 100) {
		getyzm_able = true;
		codebtn.value = len;
		codebtn.innerHTML = "获取验证码";
		codebtn.className = 'yb able';
		return;
	}
	codebtn.value = i;
	codebtn.innerHTML = (i / 1000) + "秒后重发";
	setTimeout("timedCount()", interval);
}

//是否有网
function hasNet() {
	var isNet = mobileFrame.IsNetWorkCanUse();
	return isNet;
}

//下一步
function nextStep() {
	var mobile = document.getElementById('mobile').value;
	var code = document.getElementById('code').value;
	code = trim(code);
	mobile=trim(mobile);
	if (mobile == '') {
		mui.toast('请输入手机号');
		return;
	}
	if (!issend) {
		mui.toast('请先获取验证码');
		return;
	}
	if (code == '') {
		mui.toast('请输入验证码');
		return;
	}
	
	var url = serverAjax + 'mobile/user/checkCheckCode.do';
	mui.ajax(url, {
		data: {
			code: code,
			mobile:mobile
		},
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
			if (data.code == 0) {
				//if(true){
				getyzm_able = true;
				var code = document.getElementById('code').value;
				document.getElementById('m1').className = 'mdiv';
				document.getElementById('m2').className = 'mdiv act';
			} else if(data.code == 4){
				mui.toast('当前手机号与接收验证码手机号不一致,请修改');
			}else if(data.code == 2){
				mui.toast('验证码已过期');
			}else {
				mui.toast('验证码错误');
			}
		},
		error: function(e1, e2, e3) {
			console.log(e1);
		}
	});
}
//重置密码
function savePassword() {
	if (!mobileFrame.IsNetWorkCanUse()) return;
	var mobile = document.getElementById('fMobile').value;
	var newpassword1 = document.getElementById('newpassword1').value;
	var newpassword2 = document.getElementById('newpassword2').value;
	if (newpassword1 == '') {
		mui.toast('请输入新密码');
		return;
	}
	if (newpassword2 == '') {
		mui.toast('请再次输入新密码');
		return;
	}
	if (newpassword1.length < 6 || newpassword1.length > 10) {
		mui.toast('新密码长度应该大于6小于10');
		return;
	}
	if (newpassword1 != newpassword2) {
		mui.toast('两次输入的新密码不一致');
		return;
	}
	var url = serverAjax + 'mobile/user/resetPassword.do';
	var data = {
		mobile: mobile,
		newpassword: newpassword1
	};
	mui.ajax(url, {
		data: data,
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
			if (data.type == 1) {
				mui.toast('修改密码成功,请用新密码登录');
				mui.back();
			} else if (data.type == 0) {
				// mui.toast('当前密码不正确');
			} else if (data.type == -1) {
				mui.toast('修改失败,请刷新后重试');
			}

		},
		error: function(e1, e2, e3) {
			console.log(e1);
		}
	});
}

function trim(str) {
	var strnew = str.replace(/^\s*|\s*$/g, "");
	return strnew;
}